package com.example.music;

import android.animation.ObjectAnimator;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private MusicService.MusicControl musicControl;
    private boolean isUnbind = false;  // 记录服务时候被解绑

    // 定义自定义ServiceConnection对象
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            musicControl = (MusicService.MusicControl) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    private static SeekBar seekBar;
    private static TextView tvProgress;
    private static TextView tvTotal;

    // 定义自定义Handler对象处理歌曲时间显示的消息
    public static Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            int duration = msg.arg1;
            int currentPos = msg.arg2;
            seekBar.setMax(duration);
            seekBar.setProgress(currentPos);

            int minute = duration / 1000 / 60;
            int second = duration / 1000 % 60;
            tvTotal.setText(minute + ":" + second);

            minute = currentPos / 1000 / 60;
            second = currentPos / 1000 % 60;
            tvProgress.setText(minute + ":" + second);
        }
    };

    private Intent intent;
    private ObjectAnimator animator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        // 绑定服务
        intent = new Intent(MainActivity.this, MusicService.class);
        bindService(intent, connection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        musicControl.pausePlay();
        unbindService(connection);
        stopService(intent);
    }

    private void initView() {
        seekBar = findViewById(R.id.sb_position);
        tvProgress = findViewById(R.id.tv_progress);
        tvTotal = findViewById(R.id.tv_total);

        findViewById(R.id.btn_play).setOnClickListener(this);
        findViewById(R.id.btn_pause).setOnClickListener(this);
        findViewById(R.id.btn_continue_play).setOnClickListener(this);
        findViewById(R.id.btn_exit).setOnClickListener(this);

        // 拖动条事件监听及处理
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress == seekBar.getMax()) {
                    animator.pause();
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();
                musicControl.seekTo(progress);
            }
        });
        // 设置动画
        ImageView ivMusic = findViewById(R.id.iv_music);
        animator = ObjectAnimator.ofFloat(ivMusic, "rotation", 0f, 360f);
        animator.setDuration(10000);
        animator.setInterpolator(new LinearInterpolator());
        animator.setRepeatCount(-1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_play:                //播放按钮点击事件
                musicControl.play();
                animator.start();
                break;
            case R.id.btn_pause:               //暂停按钮点击事件
                musicControl.pausePlay();
                animator.pause();
                break;
            case R.id.btn_continue_play:       //继续播放按钮点击事件
                musicControl.continuePlay();
                animator.start();
                break;
            case R.id.btn_exit:                //退出按钮点击事件
                musicControl.pausePlay();
                unbindService(connection);
                stopService(intent);
                finish();
                break;
        }
    }
}